package com.yuchting.dinneronline.server;

import java.io.Serializable;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.json.JSONObject;

import com.google.appengine.api.datastore.Text;
import com.yuchting.dinneronline.share.ShareField;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Config implements Serializable,Cloneable{
		
	public final class ConfigSegmentTime{
		long formerStart;
		long formerEnd;
		long currStart;
		long currEnd;
	}
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private String m_key = Config.class.getName();
	
	// 9:30
	@Persistent
	private int m_sheetTimeStart = (9 << 16) | 30;
	
	// 10:30
	@Persistent
	private int m_sheetTimeEnd = (10 << 16) | 30;
	
	//prompt
	@Persistent
	private String m_prompt = "这是提示";
	
	@Persistent
	private Text	m_longPrompt = new Text("这是提示");
	

	public Object clone()throws CloneNotSupportedException{
		Config newcfg = new Config();
		newcfg.m_key 			= new String(m_key);
		
		newcfg.m_sheetTimeStart = m_sheetTimeStart;
		newcfg.m_sheetTimeEnd	= m_sheetTimeEnd;
		
		newcfg.m_prompt			= new String(m_prompt);
		
		newcfg.m_longPrompt		= new Text(m_longPrompt.getValue());
		
		return newcfg;
	}
	
	public int getStartHour(){return m_sheetTimeStart >>> 16;}
	public int getStartMinutes(){return m_sheetTimeStart & 0x0000ffff;}
	
	public int getEndHour(){return m_sheetTimeEnd >>> 16;}
	public int getEndMinutes(){return m_sheetTimeEnd & 0x0000ffff;}
	
	private long getStartMillisecond(){
		return (long)getStartHour() * 3600000L +  (long)getStartMinutes() * 60000L - ShareField.TIME_ZONE * 3600000;
	}
	
	private long getEndMillisecond(){
		return (long)getEndHour() * 3600000L +  (long)getEndMinutes() * 60000L - ShareField.TIME_ZONE * 3600000;
	}
	
	private long getStartEndMillisecond(){
		return getEndMillisecond() - getStartMillisecond();
	}
	
	final static long fsm_oneDayMillisecond = (24 * 3600000);
	
	public ConfigSegmentTime getSegTime(long _currTime){
				
		ConfigSegmentTime t_time = new ConfigSegmentTime();
		
		long t_remain 	= _currTime % fsm_oneDayMillisecond;
		long t_start 	= getStartMillisecond();
				
		t_time.formerStart		= _currTime - t_remain + t_start;
		
		if(t_remain < getEndMillisecond()){
			t_time.formerStart -= fsm_oneDayMillisecond;
		}
		
		t_time.formerEnd		= t_time.formerStart + getStartEndMillisecond();
		
		t_time.currStart		= t_time.formerStart + fsm_oneDayMillisecond;
		t_time.currEnd			= t_time.formerEnd + fsm_oneDayMillisecond;

		return t_time;
	}
	
	public boolean isOrderOutTime(long _currTime){
		_currTime = _currTime % fsm_oneDayMillisecond;
		return _currTime >= getStartMillisecond() && _currTime <= getEndMillisecond();
	}
		
	public void setStartTime(int _hour,int _minutes){
		m_sheetTimeStart = (_hour << 16) | _minutes;
	}
	
	public void setEndTime(int _hour,int _minutes){
		m_sheetTimeEnd = (_hour << 16) | _minutes;
	}
	
	public String getPrompt(){
		return m_longPrompt == null ? "":m_longPrompt.getValue();
	}
	public void setPrompt(String _prompt){
		m_longPrompt = new Text(_prompt);
	}
	
	public JSONObject toJSON(){
		
		JSONObject t_result = new JSONObject();
		try{
			t_result.put("start",m_sheetTimeStart);
			t_result.put("end",m_sheetTimeEnd);
			t_result.put("prompt",getPrompt());
		}catch(Exception e){
			System.err.println("Config toJSON Error:");
			e.printStackTrace(System.err);
		}
		
		return t_result;
	}
	
	public void fromJSON(JSONObject _json){
		try{
			m_sheetTimeStart = _json.getInt("start");
			m_sheetTimeEnd 	 = _json.getInt("end");
			
			setPrompt(_json.getString("prompt"));
			
		}catch(Exception e){
			System.err.println("Config fromJSON Error:");
			e.printStackTrace(System.err);
		}
	}
	
}